<?php
class UserModel extends Model{

	public $table = 'user';
	
	//自动验证
	public $validate = array(			
			array('password', 'nonull', '密码不能为空 ',2,3),
			array('code', 'nonull', ' 验证码不能为空 ',2,3),
			array('password','confirm:password2',' 两次密码不一致 ',2,3),
	);
	//自动完成
	public $auto=array(
	  array('reg_time','time','function',2,3),
	  array('login_time','time','function',2,3)
	);
	//前提用户注册
	public function reg(){
		//触发自动验证
		if(!$this->create())return false;
		//验证码 验证
		if(Q('post.code','','strtoupper') != session('code')){
			$this->error = '验证码不正确';
			return false;
		}	
		//注册处理
		//查询邮箱和手机是否存在			
			$email =Q('post.email');
			$tel =Q('post.tel');
			//如果email有post值 就将email值付给 登录用户名   相反查看tel信息 如果存在 赋值给登录用户名  插入数据  都没有则返回二次验证返回
			if(!empty($email)){				
				$email = $this->where("username='{$email}'")->find();
				if(!$email){					
					$_POST['username'] = $_POST['email'];
					$_POST['password'] = md5($_POST['password']);					
					$userinfo = $this->add();
					session('username',$_POST['username']);
					session('uid',$userinfo);
					setcookie(session_name(),session_id(),0,'/');
				}else{
				 	$this->error="邮箱已存在";
					return false;
				}			
			}else if(!empty($tel)){				
				$tels =(int)$tel;				
				if(mb_strlen($tels)< 10 || mb_strlen($tels)>10){
					$this->error="手机号码格式不正确";
					return false;
				}
				
				$tel = $this->where("username='{$tel}'")->find();				
				if(!$tel){
					$_POST['username'] = $_POST['tel'];
					$_POST['password'] = md5($_POST['password']);
					$_POST['reg_time'] = time();
					$userinfo = $this->add();
					session('username',$_POST['username']);
					session('uid',$userinfo);
					setcookie(session_name(),session_id(),0,'/');
				}else{
				 	$this->error="手机已存在";
					return false;
				}
			}else{
				$this->error="手机和邮箱请务必填写一项";
				return false;
			};
			

		return true;
	}
	//用户登录
	public function login(){
		if(IS_POST){
			//提交信息判断是否空
			if(Q('post.username') == ''  || Q('post.password') == ''){
				$this->error = "请将信息填写完整后再次尝试提交！";
				return false;
			}
			//用户名密码验证
			$username = Q('post.username');
			$userInfo = $this->where("username = '{$username}'")->find();
			//查询信息部存在 返回错误提示
			if(!$userInfo){
				$this->error = "用户名或密码错误！";
				return false;
			}else{
				if($userInfo['username'] == Q('post.username')  && $userInfo['password'] == md5(Q('post.password'))){
					//写入session 记录登录时间  如果设置自动登录 设置保存呢cook时间
					session('username',$userInfo['username']);
					session('uid',$userInfo['uid']);
					if(isset($_POST['auto'])){						
						setcookie(session_name(),session_id(),time()+33600*24*7,'/');
					}else{
						setcookie(session_name(),session_id(),0,'/');
					}
					return true;
				}else{
					$this->error="用户名或密码错误！";
					return false;
				}
				
			}
		}

	}
	//检查用户用是否存在
	public function cheackUsername(){
		if(IS_POST){
			if(isset($_POST['username']) && Q('post.username') != ''){
				$username = Q('post.username');
				$userinfo = $this->where("username='{$username}'")->find();
				if($userinfo){
					echo 1;die;
				}else{
					echo 0;die;
				}
			}

		}
	}
	//检查验证码是否真确
	public function cheackCode(){
		if(IS_POST){
			if(isset($_POST['codeval']) && Q('post.codeval') != ''){
				$code = Q('post.codeval');
					if(session('code') == strtoupper($code)){					
						echo 0;die;
					}else{
						echo 1;die;
					
				}
			}
		}
	}
	
	
}
